<template>
  <base-dialog
    :title="title + '部门'"
    ref="baseDialog"
    @handleClose="handleClose"
    @handleSubmit="handleSubmit"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="95px">
      <el-form-item label="上级部门" prop="parentId">
        <treeselect
          v-model="form.parentId"
          :options="treeDataList"
          :normalizer="normalizer"
          placeholder="请选择上级部门"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="form.deptName"
          placeholder="请输入部门名称"
          show-word-limit
          clearable
          :maxlength="20"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="deptLeader">
        <el-input
          v-model="form.deptLeader"
          placeholder="请选择部门负责人"
          show-word-limit
          clearable
          :maxlength="20"
          @focus="peopleSelect"
        />
      </el-form-item>
      <el-form-item label="排序" prop="orderNo">
        <el-input-number
          v-model="form.orderNo"
          :style="{ width: '100%' }"
          controls-position="right"
          :min="0"
          :max="999"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          placeholder="请输入备注"
          show-word-limit
          clearable
          :autosize="{ minRows: 2, maxRows: 12 }"
          :maxlength="200"
        />
      </el-form-item>
    </el-form>
    <peopleSelect ref="peopleSelect" @selectPeople="selectPeopleEmit"></peopleSelect>
  </base-dialog>
</template>

<script>
import peopleSelect from "./peopleSelect"
import { add, edit } from "@/api/system/dept"
import Treeselect from "@/plugins/Treeselect"
import { mapGetters } from "vuex"
export default {
  name: "Dialog",
  components: {
    Treeselect,
    peopleSelect
  },
  props: ["dataList", "getList"],
  data() {
    return {
      title: "",
      form: {
        deptLeader: "",
        parentId: ""
      },
      rules: {
        parentId: [{ required: true, message: "请选择上级部门", trigger: "blur" }],
        deptName: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
        deptLeader: [{ required: true, message: "请输入部门负责人", trigger: "change" }],
        orderNo: [{ required: true, message: "请输入序号", trigger: "blur" }]
      },
      treeDataList: []
    }
  },
  computed: {
    ...mapGetters(["token"])
  },
  methods: {
    init(item, title) {
      this.title = title
      this.$refs.baseDialog.dialogVisible = true
      this.initTreeList()
      if (item) {
        if (this.title === "编辑") {
          this.form = JSON.parse(JSON.stringify(item))
        } else {
          this.form.parentId = item.deptId
        }
      } else {
        this.form.parentId = "0"
      }
    },
    initTreeList() {
      const tree = { deptId: "0", deptName: "根部门", children: this.dataList }
      // tree.children = this.dataList
      this.treeDataList = [tree]
    },
    handleClose() {
      this.form = {
        deptLeader: ""
      }
      this.$refs.baseDialog.dialogVisible = false
      this.title = ""
    },
    peopleSelect() {
      this.$refs.peopleSelect.init(this.form.deptLeader)
    },
    selectPeopleEmit(val) {
      this.form.deptLeader = val.username
    },
    // 提交表单
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.title === "编辑") {
            const { data } = await edit(this.form)
            if (!data) return
            this.$message({
              type: "success",
              message: "部门编辑成功"
            })
            this.$emit("updatePage")
            this.handleClose()
          } else {
            const { data } = await add(this.form)
            if (!data) return
            this.$message({
              type: "success",
              message: "部门新增成功"
            })
            this.$emit("updatePage")
            this.handleClose()
          }
        }
      })
    },
    // 后台返回的数据如果和VueTreeselect要求的数据结构不同，需要进行转换
    normalizer(node) {
      // 去掉children=[]的children属性
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
